{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ndarray数组的创建方式",
   "id": "89a71c6513a7925a"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:32:35.783371Z",
     "start_time": "2025-08-25T07:32:35.695167Z"
    }
   },
   "source": "import numpy as np",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1. np.array(list)  把列表转换成数组",
   "id": "6a51d6ea94405594"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T07:36:05.458582Z",
     "start_time": "2025-08-25T07:36:05.450Z"
    }
   },
   "cell_type": "code",
   "source": [
    "my_list1 = [1,2,3,4,5]\n",
    "a = np.array(my_list1)\n",
    "\n",
    "print(my_list1,type(my_list1))\n",
    "print(a,type(a))\n",
    "\n",
    "b = np.array(range(1,5))\n",
    "print(b,type(b))"
   ],
   "id": "d4c1a718ce5a824c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] <class 'list'>\n",
      "[1 2 3 4 5] <class 'numpy.ndarray'>\n",
      "[1 2 3 4] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. np.arange(start,stop,step) 类似于range(),返回数组",
   "id": "d2c4396d5c7ab10c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T07:40:06.844103Z",
     "start_time": "2025-08-25T07:40:06.836588Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 用arange函数创建数组\n",
    "arr1 = np.arange(1,5) # 含头不含尾，不写步长，步长默认为1\n",
    "print(arr1,type(arr1))\n",
    "\n",
    "arr2 = np.arange(1,10,2)\n",
    "print(arr2,type(arr2)) # 步长为2"
   ],
   "id": "f286a1525be9dcb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4] <class 'numpy.ndarray'>\n",
      "[1 3 5 7 9] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 3.np.zeros(shape,dtype)创建全0数组\n",
    "- shape 数组元素个数\n",
    "- dtype  类型"
   ],
   "id": "f9a0660a40b63fc5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T07:48:37.676544Z",
     "start_time": "2025-08-25T07:48:37.666959Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr3 = np.zeros(10) # 创建10个一维的全0数组\n",
    "print(arr3,type(arr3))\n",
    "\n",
    "print('-' * 10)\n",
    "\n",
    "arr4 = np.zeros([3,5]) # 创建3行5列的全0数组\n",
    "print(arr4,type(arr4))\n",
    "\n",
    "print('-' * 10)\n",
    "\n",
    "arr5 = np.zeros([3,5],int)\n",
    "print(arr5,type(arr5))"
   ],
   "id": "b6ef11c299b722c7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] <class 'numpy.ndarray'>\n",
      "----------\n",
      "[[0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0.]] <class 'numpy.ndarray'>\n",
      "----------\n",
      "[[0 0 0 0 0]\n",
      " [0 0 0 0 0]\n",
      " [0 0 0 0 0]] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 4.np.ones(shape,dtype)创建全1数组\n",
    "- shape 数组元素个数\n",
    "- dtype  类型"
   ],
   "id": "a36e37c4b1beb16d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T07:48:20.376993Z",
     "start_time": "2025-08-25T07:48:20.368516Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr6 = np.ones(10)\n",
    "print(arr6,type(arr6))\n",
    "\n",
    "print('-' * 10)\n",
    "arr7 = np.ones([3,5])\n",
    "print(arr7,type(arr7))\n",
    "\n",
    "print('-' * 10)\n",
    "arr8 = np.ones([3,5],int)\n",
    "print(arr8,type(arr8))"
   ],
   "id": "4c6c9493936d7ca6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] <class 'numpy.ndarray'>\n",
      "----------\n",
      "[[1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1.]] <class 'numpy.ndarray'>\n",
      "----------\n",
      "[[1 1 1 1 1]\n",
      " [1 1 1 1 1]\n",
      " [1 1 1 1 1]] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 5. np.zeros_like(a,dtype='类型') 创建形状相同的数组\n",
    "- a 的形状和数据类型来定义返回数组的属性\n",
    "- dtype:数据类型，可选\n"
   ],
   "id": "5e9d7b74a21cf36e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T07:54:03.319148Z",
     "start_time": "2025-08-25T07:54:03.311654Z"
    }
   },
   "cell_type": "code",
   "source": [
    "aa = np.array([\n",
    "    [1,2,3,4],\n",
    "    [6,5,4,4],\n",
    "    [7,4,1,4]\n",
    "])\n",
    "print(aa,type(aa))\n",
    "\n",
    "print('-' * 10)\n",
    "\n",
    "arr9 = np.zeros_like(aa)\n",
    "print(arr9,type(arr9))\n"
   ],
   "id": "d8551f77008f2f9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4]\n",
      " [6 5 4 4]\n",
      " [7 4 1 4]] <class 'numpy.ndarray'>\n",
      "----------\n",
      "[[0 0 0 0]\n",
      " [0 0 0 0]\n",
      " [0 0 0 0]] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 6. np.ones_like(a,dtype='类型') 创建形状相同的数组\n",
    "- a 的形状和数据类型来定义返回数组的属性\n",
    "- dtype:数据类型，可选"
   ],
   "id": "6ce5e9cb293cbbcf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T07:55:19.996693Z",
     "start_time": "2025-08-25T07:55:19.989355Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建一个和aa形状相同的数组\n",
    "arr10 = np.ones_like(aa)\n",
    "print(arr10,type(arr10))"
   ],
   "id": "428ced488b57c767",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1 1]\n",
      " [1 1 1 1]\n",
      " [1 1 1 1]] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 7. np.full(shape,fill_value,dtype=None)创建指定值的数组\n",
    "- shape:整数或者整形元组定义返回数组的形状，可以是一个数（创建一维向量），也可以是一个元组（创建多维向量）\n",
    "- fill_value：标量（就是纯数值变量）\n",
    "- dtype:数据类型，可选，定义返回数组的类型。\n"
   ],
   "id": "c52e2d66dd50826d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T08:07:21.485144Z",
     "start_time": "2025-08-25T08:07:21.477846Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr11 = np.full(5,520,int)\n",
    "print(arr11,type(arr11))\n",
    "\n",
    "print('-' * 10)\n",
    "\n",
    "arr12 = np.full([3,5],5,int)\n",
    "print(arr12,type(arr12))"
   ],
   "id": "9d9b150a0332f173",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[520 520 520 520 520] <class 'numpy.ndarray'>\n",
      "----------\n",
      "[[5 5 5 5 5]\n",
      " [5 5 5 5 5]\n",
      " [5 5 5 5 5]] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 7. np.full_like(a,fill_value,dype=None)",
   "id": "61e1f0d3272fb229"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T08:10:22.833775Z",
     "start_time": "2025-08-25T08:10:22.825889Z"
    }
   },
   "cell_type": "code",
   "source": [
    "bb = np.array([\n",
    "    [1,2,3,4],\n",
    "    [6,5,4,4],\n",
    "    [7,4,1,4]\n",
    "])\n",
    "\n",
    "arr13 = np.full_like(bb,520)\n",
    "print(arr13,type(arr13))"
   ],
   "id": "9f876ba8c3d44a4e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[520 520 520 520]\n",
      " [520 520 520 520]\n",
      " [520 520 520 520]] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 8. np.empty(shape,dtype)  创建未初始化的数组",
   "id": "b0a32a0cc6b3e140"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T08:12:05.242383Z",
     "start_time": "2025-08-25T08:12:05.234378Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr14 = np.empty(2,int)\n",
    "print(arr14,type(arr14))"
   ],
   "id": "d8ce91e39671a766",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2035487338576             0] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 9.使用random模块生成随机数组\n",
    "- np.random.randn(d0,d1,......,dn)\n",
    "- 传1个数就是一维，2个数就是二维，n个数就是n维"
   ],
   "id": "ac8b365f0a93061b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T08:16:39.547088Z",
     "start_time": "2025-08-25T08:16:39.539472Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr15 = np.random.randn(2) # 长度为2的一维数组\n",
    "print(arr15,type(arr15))\n",
    "\n",
    "print('-'*10)\n",
    "arr16 = np.random.randn(3,3) # 创建3行3列的二维数组\n",
    "print(arr16,type(arr16))\n"
   ],
   "id": "ce616405ffa69aa7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.61591949 -1.82013908] <class 'numpy.ndarray'>\n",
      "----------\n",
      "[[-1.06198648 -2.15252906 -0.9176049 ]\n",
      " [ 1.34980344 -0.2321434   0.29391185]\n",
      " [ 0.83610091  0.01866809 -2.04601897]] <class 'numpy.ndarray'>\n",
      "----------\n",
      "2 <class 'int'>\n"
     ]
    }
   ],
   "execution_count": 53
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
